package apis

import (
	"fmt"
	"vrcm/src/vrcm/app/admin/models/comment"
	"vrcm/src/vrcm/common"
	"vrcm/src/vrcm/common/apis"

	"github.com/gin-gonic/gin"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"vrcm/src/vrcm/app/admin/service/comment"
	"vrcm/src/vrcm/app/admin/service/dto/comment"
	"vrcm/src/vrcm/common/actions"
)

type Comment struct {
	apis.Api
}

func (e Comment) GetPage(c *gin.Context) {
	req := dto.CommentGetPageReq{}
	s := service.Comment{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]comment.Comment, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取Comment失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取Comment
// @Summary 获取Comment
// @Description 获取Comment
// @Tags Comment
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.Comment} "{"code": 200, "data": [...]}"
// @Router /api/v1/comment/{id} [get]
// @Security Bearer
func (e Comment) Get(c *gin.Context) {
	req := dto.CommentGetReq{}
	s := service.Comment{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object comment.Comment

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取Comment失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建Comment
// @Summary 创建Comment
// @Description 创建Comment
// @Tags Comment
// @Accept application/json
// @Product application/json
// @Param data body dto.CommentInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/comment [post]
// @Security Bearer
func (e Comment) Insert(c *gin.Context) {
	req := dto.CommentInsertReq{}
	s := service.Comment{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	//err = s.NoSpeak(&req)
	//if err != nil {
	//	e.Logger.Error(err)
	//	e.Error(apis.NoSpeakCommentError.Code, err, err.Error())
	//	return
	//}

	ip := common.GetClientIP(c)
	req.CommentIp = ip
	err = s.Insert(&req)
	if err != nil {
		e.Error(apis.AddCommentError.Code, err, fmt.Sprintf("创建Comment失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改Comment
// @Summary 修改Comment
// @Description 修改Comment
// @Tags Comment
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.CommentUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/comment/{id} [put]
// @Security Bearer
func (e Comment) Update(c *gin.Context) {
	req := dto.CommentUpdateReq{}
	s := service.Comment{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改Comment失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除Comment
// @Summary 删除Comment
// @Description 删除Comment
// @Tags Comment
// @Param data body dto.CommentDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/comment [delete]
// @Security Bearer
func (e Comment) Delete(c *gin.Context) {
	s := service.Comment{}
	req := dto.CommentDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除Comment失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// Review 审核
func (e Comment) Review(c *gin.Context) {
	s := service.Comment{}
	req := dto.CommentReviewReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Review(&req)
	if err != nil {
		e.Error(apis.ReviewCommentError.Code, err, fmt.Sprintf("审核Comment失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "审核成功")
}

// GetValidateComments 获取所有评论
func (e Comment) GetValidateComments(c *gin.Context) {
	s := service.Comment{}
	req := dto.GetAllReviews{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.GetCommentError.Code, err, err.Error())
		return
	}

	data := s.GetValidateComments(&req)
	e.OK(data, "查询成功")
}

func (e Comment) GetReplies(c *gin.Context) {
	s := service.Comment{}
	req := dto.GetAllReplies{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.GetCommentError.Code, err, err.Error())
		return
	}

	data, err := s.GetReplies(&req)
	if err != nil {
		e.Error(apis.ReviewCommentError.Code, err, fmt.Sprintf("获取评论失败， %s", err.Error()))
		return
	}
	e.OK(data, "查询成功")
}

func (e Comment) GetCommentList(c *gin.Context) {
	s := service.Comment{}
	req := dto.GetCommentList{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.GetCommentError.Code, err, err.Error())
		return
	}

	var count int64
	data, err := s.GetCommentList(&req, &count)
	if err != nil {
		e.Error(apis.ReviewCommentError.Code, err, fmt.Sprintf("获取评论失败， %s", err.Error()))
		return
	}
	e.PageOK(data, int(count), req.PageIndex, req.PageSize, "查询成功")
}

func (e Comment) DeleteComment(c *gin.Context) {
	s := service.Comment{}
	req := dto.DeleteComment{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.GetCommentError.Code, err, err.Error())
		return
	}

	err = s.DeleteComment(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除评论失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除评论成功")
}
